/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-25
*/
package com.rzico.order.entity;

import com.rzico.annotation.IgnoreSwaggerParameter;
import com.rzico.basics.entity.Shop;
import com.rzico.core.entity.SysEmployee;
import com.rzico.core.entity.SysOffice;
import com.rzico.order.enumx.AllocateShippingStatusEnum;
import com.rzico.order.enumx.AllocateStatusEnum;
import com.rzico.order.enumx.PurchaseOrderStatusEnum;
import com.rzico.order.enumx.PurchaseShippingStatusEnum;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
import io.swagger.annotations.ApiModelProperty;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * <pre>
 *   调拨单数据库对象
 * </pre>
 * @author Rzico Boot
 * @version 1.0
 */
@Data
@JsonIgnoreProperties(value = {"handler"})
@JsonInclude(JsonInclude.Include.NON_NULL)
@Table(name = "wx_allocate")
public class Allocate{
    @Id
    @ApiModelProperty(value = "" )
    protected Long id;

    @ApiModelProperty(value = "调拨单号" )
    protected String sn;

    @ApiModelProperty(value = "创建日期" )
    protected java.util.Date createDate;

    @ApiModelProperty(value = "修改日期" )
    protected java.util.Date modifyDate;

    @ApiModelProperty(value = "调拨日期" )
    protected java.util.Date orderDate;

    @ApiModelProperty(value = "备注" )
    protected String memo;

    @ApiModelProperty(value = "所属部门" )
    protected String officeId;

    @ApiModelProperty(value = "调拨企业" )
    protected Long enterpriseId;

    @ApiModelProperty(value = "调入门店" )
    protected Long inShopId;

    @ApiModelProperty(value = "调出门店" )
    protected Long outShopId;

    @ApiModelProperty(value = "单据状态(0、未确认 1、已确认、2、已完成 3、已取消)" )
    protected Integer orderStatus;

    @ApiModelProperty(value = "出入库状态(1、待出库 2、已出库 3、已入库)" )
    protected Integer shippingStatus;

    @JsonIgnore
    @ApiModelProperty(value = "创建者" ,hidden = true )
    protected String createBy;

    @JsonIgnore
    @ApiModelProperty(value = "更新者" ,hidden = true )
    protected String modifyBy;

    @ApiModelProperty(value = "操作人")
    protected String operatorId;

    @ApiModelProperty(value = "调拨明细")
    List<AllocateItem> itemList = Collections.emptyList();

    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public BigDecimal calculateSubQuantity(){
        BigDecimal totalQuantity = new BigDecimal(0);
        if (null != itemList && itemList.size() > 0){
            for (AllocateItem allocateItem : itemList){
                totalQuantity = totalQuantity.add(allocateItem.getQuantity());
            }
        }
        return totalQuantity;
    }

    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public BigDecimal calculateSubPrice(){
        BigDecimal totalSubPrice = new BigDecimal(0);
        if (null != itemList && itemList.size() > 0){
            for (AllocateItem allocateItem : itemList){
                BigDecimal subPriceTotal = allocateItem.getPrice().multiply(allocateItem.getQuantity());
                totalSubPrice =  totalSubPrice.add(subPriceTotal);
            }
        }
        return totalSubPrice;
    }

    @JsonIgnore
    @Column(insertable = false)
    @Transient
    public String flag;

    /**
     * 存放入库单ID
     */
    @JsonIgnore
    @Column(insertable = false)
    @Transient
    public Long warehouseId;

    @JsonIgnore
    @IgnoreSwaggerParameter
    public Shop inShop;

    @JsonIgnore
    @IgnoreSwaggerParameter
    public Shop outShop;

    @JsonIgnore
    @IgnoreSwaggerParameter
    public SysOffice sysOffice;

    @ApiModelProperty(value = "入库门店名称", hidden = true)
    public String getInShopName(){
        Shop inShop = getInShop();
        if (null != inShop){
            return inShop.getName();
        }else{
            return "";
        }
    }

    public void setInShopName(String inShopName){

    }

    @ApiModelProperty(value = "出库门店名称", hidden = true)
    public String getOutShopName(){
        Shop outShop = getOutShop();
        if (null != outShop){
            return outShop.getName();
        }else{
            return "";
        }
    }

    public void setOutShopName(String outShopName){

    }

    @ApiModelProperty(value = "部门名称", hidden = true)
    public String getOfficeName(){
        SysOffice sysOffice = getSysOffice();
        if (null != sysOffice){
            return sysOffice.getName();
        }else{
            return "";
        }
    }

    public void setOfficeName(String officeName){

    }

    @JsonIgnore
    @IgnoreSwaggerParameter
    public SysEmployee sysEmployee;

    @ApiModelProperty(value = "人员名称", hidden = true)
    public String getOperatorName(){
        SysEmployee sysEmployee = getSysEmployee();
        if (null != sysEmployee){
            return sysEmployee.getName();
        }else{
            return "";
        }
    }

    public void setOperatorName(String operatorName){

    }


    @ApiModelProperty(value = "状态")
    public String getStatusDescr() {
        if (getOrderStatus().equals(AllocateStatusEnum.ALLOCATE_STATUS_CANCLE.getId()) ) {
            return "已关闭";
        } else
        if (getOrderStatus().equals(AllocateStatusEnum.ALLOCATE_STATUS_UNCONFIRM.getId())) {
            return "未确认";
        } else
        if (getOrderStatus().equals(AllocateStatusEnum.ALLOCATE_STATUS_CONFIRM.getId()) && getShippingStatus().equals(AllocateShippingStatusEnum.ALLOCATE_SHIPPING_HAS_IN.getId()) ) {
            return "已入库";
        } else
        if (getOrderStatus().equals(AllocateStatusEnum.ALLOCATE_STATUS_CONFIRM.getId()) && getShippingStatus().equals(AllocateShippingStatusEnum.ALLOCATE_SHIPPING_HAS_OUT.getId()) ) {
            return "已出库";
        } else
        if (getOrderStatus().equals(AllocateStatusEnum.ALLOCATE_STATUS_CONFIRM.getId()) && getShippingStatus().equals(AllocateShippingStatusEnum.ALLOCATE_SHIPPING_WAIT_OUT.getId()) ) {
            return "待出库";
        } else
        if (getOrderStatus().equals(AllocateStatusEnum.ALLOCATE_STATUS_COMPLETE.getId())) {
            return "已完成";
        } else {
            return "未知状态";
        }
    }

    public void setStatusDescr(String value) {

    }
}
